package com.example.culture.service.serviceimpl;

import com.example.culture.common.ServerResponse;
import com.example.culture.domain.Classes;
import com.example.culture.domain.Group;
import com.example.culture.mapper.AdminMapper;
import com.example.culture.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {


    @Autowired
    public AdminMapper adminMapper;

    @Override
    public ServerResponse addClasses(Classes classes) {
        int num=0;
        try {
            num=adminMapper.addClasses(classes);//先添加班级信息
            List<Group> groups=classes.getGroup();//取出班级对象中的所有分组信息
            if(groups.size()!=0){
                for (int i = 0; i < groups.size(); i++) {
                    groups.get(i).setClassesId(classes.getClassesId());
                }
                adminMapper.addAllGroup(groups);//添加所有的分组信息
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("添加班级分组失败");
        }
        if(num!=0){
            return ServerResponse.createBySuccessMsg("成功添加班级分组");

        }else{
            return ServerResponse.createByErrorMsg("添加班级分组失败");

        }
    }

    @Override
    public ServerResponse updateClass(Classes classes) {
        try {
            adminMapper.updateClass(classes);
            List<Group> groupList=classes.getGroup();
            if(groupList.size()!=0){
                for (int i = 0; i < groupList.size(); i++) {
                    Group group=groupList.get(i);
                    adminMapper.updateGroup(group);
                }
            }
            return ServerResponse.createBySuccessMsg("成功修改班级分组信息");
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("修改修改班级分组信息失败");
        }
    }

    @Override
    public ServerResponse updateGroup(Group group) {
        int checkUpdateGroup=adminMapper.updateGroup(group);
        if(checkUpdateGroup!=0){
            return ServerResponse.createBySuccessMsg("成功修改分组名称");
        }else {
            return ServerResponse.createByErrorMsg("修改分组名称失败");
        }
    }

    @Transactional
    @Override
    public ServerResponse deleteClasses(Integer classesId) {
        adminMapper.deleteGroup(classesId);
        int deleteClassesStatus=adminMapper.deleteClasses(classesId);
        if(deleteClassesStatus!=0){
            return ServerResponse.createBySuccessMsg("成功删除班级信息");
        }else{
            return ServerResponse.createByErrorMsg("删除班级信息失败");
        }
    }

    @Override
    public ServerResponse deleteGroup(Integer groupId) {
        int deleteGroupStatus=adminMapper.deleteGroupByGroupId(groupId);
        if(deleteGroupStatus!=0){
            return ServerResponse.createBySuccessMsg("成功删除分组信息");
        }else{
            return ServerResponse.createByErrorMsg("删除分组信息失败");
        }
    }
}
